package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Menu;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IMenuService;
import cn.wolfcode.utils.RequestPermission;
import cn.wolfcode.utils.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService ;

    @Autowired
    private IMenuService menuService;

    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/list")
    @RequestPermission(name="部门查询",expression = "department:list")
    public String list(Model model , QueryObject qo){


        PageInfo<Department> pageInfo = departmentService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "department/list" ;
    }

    @RequestMapping("/delete")
    @RequestPermission(name="部门删除",expression = "department:delete")
    public String delete(Long id){
        if (id != null) {
            departmentService.delete(id);
        }
        return "redirect:/department/list" ;
    }


    @RequestMapping("/input")
    public String input(Long id,Model model){
        if (id != null) {
            model.addAttribute("department",departmentService.selectById(id));
        }
        return "/department/input" ;
    }

    @RequestMapping("/save")
    @RequestPermission(name="部门编辑",expression = "department:save")
    public String save(Department department , Long id){
        if (id != null){
            departmentService.update(department);
            return "redirect:/department/list";
        }
        departmentService.insert(department);
        return "redirect:/department/list";
    }
}
